package com.har.modules.business.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.har.common.utils.PageUtils;
import com.har.common.utils.R;
import com.har.common.validator.ValidatorUtils;
import com.har.modules.business.common.CodeConstant;
import com.har.modules.business.entity.TBaseProductEntity;
import com.har.modules.business.entity.TFinanceProductEntity;
import com.har.modules.business.service.TBaseProductService;
import com.har.modules.business.service.TFinanceProductService;
import com.har.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 智慧门店
 *
 * @author macwy
 * @email
 * @date 2018-06-26 16:38:42
 */
@RestController
@RequestMapping("business/store")
public class StoreController {
    @Autowired
    private TFinanceProductService tFinanceProductService;

    @Autowired
    private TBaseProductService baseProductService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:store:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("productType", CodeConstant.PRODUCT_TYPE.STORE.getCode());
        PageUtils page = tFinanceProductService.queryPage(params);

        String[] types = null;

        TBaseProductEntity tBaseProductEntity =baseProductService.getOneByProductType(CodeConstant.PRODUCT_TYPE.STORE.getCode());

        if(null !=tBaseProductEntity&& null != tBaseProductEntity.getType()){
            types = tBaseProductEntity.getType().split(",");
        }

        return R.ok().put("page", page).put("baseLoan",tBaseProductEntity).put("types",types);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    @RequiresPermissions("business:store:info")
    public R info(){

        EntityWrapper<TFinanceProductEntity> ew = new EntityWrapper<>();
        ew.eq("product_type",CodeConstant.PRODUCT_TYPE.STORE.getCode());
        TFinanceProductEntity tFinanceProduct = tFinanceProductService.selectOne(ew);

        TBaseProductEntity tBaseProductEntity =baseProductService.getOneByProductType(CodeConstant.PRODUCT_TYPE.STORE.getCode());

        return R.ok().put("tFinanceProduct", tFinanceProduct).put("baseLoan",tBaseProductEntity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:store:save")
    public R save(@RequestBody TFinanceProductEntity tFinanceProduct){

        TBaseProductEntity tBaseProductEntity =baseProductService.getOneByProductType(CodeConstant.PRODUCT_TYPE.STORE.getCode());
        if(null == tBaseProductEntity){
            return  R.error("请先添加基础配置");
        }

        EntityWrapper<TFinanceProductEntity> ew = new EntityWrapper<>();
        ew.eq("product_type",CodeConstant.PRODUCT_TYPE.STORE.getCode());
        int totalCount = tFinanceProductService.selectCount(ew);
        if(totalCount == 1){
            return  R.error("产品配置只能存在一条数据");
        }

        tFinanceProduct.setProductTypeName(CodeConstant.PRODUCT_TYPE.STORE.getMsg());
        tFinanceProduct.setProductType(CodeConstant.PRODUCT_TYPE.STORE.getCode());
        tFinanceProduct.setCreateTime(new Date());
        tFinanceProduct.setUpdateTime(new Date());
        tFinanceProduct.setOperator(ShiroUtils.getUserEntity().getUsername());
        tFinanceProduct.setStatus(CodeConstant.BANNER_STATUS.CLOSE.getCode());
        tFinanceProduct.setProductId(tBaseProductEntity.getId());

        tFinanceProductService.insert(tFinanceProduct);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:store:update")
    public R update(@RequestBody TFinanceProductEntity tFinanceProduct){
        ValidatorUtils.validateEntity(tFinanceProduct);

        TBaseProductEntity tBaseProductEntity =baseProductService.getOneByProductType(CodeConstant.PRODUCT_TYPE.STORE.getCode());
        if(null == tBaseProductEntity){
            return  R.error("请先添加基础配置");
        }
        tFinanceProduct.setProductTypeName(CodeConstant.PRODUCT_TYPE.STORE.getMsg());
        tFinanceProduct.setProductType(CodeConstant.PRODUCT_TYPE.STORE.getCode());
        tFinanceProduct.setUpdateTime(new Date());
        tFinanceProduct.setOperator(ShiroUtils.getUserEntity().getUsername());
        tFinanceProduct.setProductId(tBaseProductEntity.getId());
        tFinanceProductService.updateAllColumnById(tFinanceProduct);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("business:store:delete")
    public R delete(@PathVariable(value = "id") Long id){
        tFinanceProductService.deleteBatchIds(Arrays.asList(id));

        return R.ok();
    }


    /**
     * 基础配置保存
     * @param tBaseProductEntity
     * @return
     */
    @RequestMapping("/baseSave")
    public R baseSave(@RequestBody TBaseProductEntity tBaseProductEntity){
        EntityWrapper<TBaseProductEntity> ew = new EntityWrapper<>();
        ew.eq("product_type",CodeConstant.PRODUCT_TYPE.STORE.getCode());
        int totalCount = baseProductService.selectCount(ew);

        if(totalCount ==1){
            return  R.error("基础配置只能有一条数据");
        }



        tBaseProductEntity.setUpdateTime(new Date());
        tBaseProductEntity.setOperator(ShiroUtils.getUserEntity().getUsername());
        tBaseProductEntity.setCreateTime(new Date());
        tBaseProductEntity.setProductType(CodeConstant.PRODUCT_TYPE.STORE.getCode());
        tBaseProductEntity.setProductTypeName(CodeConstant.PRODUCT_TYPE.STORE.getMsg());
        baseProductService.insert(tBaseProductEntity);

        return  R.ok();
    }

    /**
     * 基础配置更新
     * @param tBaseProductEntity
     * @return
     */
    @RequestMapping("/baseUpdate")
    public R baseUpdate(@RequestBody TBaseProductEntity tBaseProductEntity){

        tBaseProductEntity.setUpdateTime(new Date());
        tBaseProductEntity.setOperator(ShiroUtils.getUserEntity().getUsername());
        tBaseProductEntity.setProductType(CodeConstant.PRODUCT_TYPE.STORE.getCode());
        tBaseProductEntity.setProductTypeName(CodeConstant.PRODUCT_TYPE.STORE.getMsg());
        baseProductService.updateAllColumnById(tBaseProductEntity);

        return  R.ok();
    }




}
